// Ionic Starter App
// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers','starter.services'],function ($httpProvider){
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
    var param = function(obj) {
        var query = '', name, value, fullSubName, subName, subValue, innerObj, i;
        for(name in obj) {
            value = obj[name];
            
            if(value instanceof Array) {
                for(i=0; i<value.length; ++i) {
                    subValue = value[i];
                    fullSubName = name + '[' + i + ']';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }
            }
            else if(value instanceof Object) {
                for(subName in value) {
                    subValue = value[subName];
                    fullSubName = name + '[' + subName + ']';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }
            }
            else if(value !== undefined && value !== null)
            query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
        }
          
        return query.length ? query.substr(0, query.length - 1) : query;
    };

    // Override $http service's default transformRequest
    $httpProvider.defaults.transformRequest = [function(data) {
        return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
    }];
})

.run(function($ionicPlatform) {
    $ionicPlatform.ready(function() {
        // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
        // for form inputs)
        if(window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if(window.StatusBar) {
            // org.apache.cordova.statusbar required
            StatusBar.styleDefault();
        }
    });
})

.config(function($stateProvider, $urlRouterProvider) {
	recommend = getQueryStringByName("recommend");
    // platform = getQueryStringByName("pctype");
    appId = getQueryStringByName("proid");    $stateProvider
    .state('menu', {
        url: "/menu",
        abstract: true,
        templateUrl: "templates/menu.html",
        controller:'MainCtrl'
    })
    //全部客户
    .state('menu.list', {
        url: "/list",
        views: {
            'menuContent' :{
                templateUrl: "templates/user_list.html"
            }
        }
    })
    //客户标签
    .state('menu.label', {
        url: "/label",
        views: {
            'menuContent' :{
                templateUrl: "templates/label_list.html"
            }
        }
    })
    if('pad' == platform){
        $stateProvider
        //客户详情
        .state('menu.list.user_details', {
            url: "/user_details/:id",
            views: {
                'dataContent' :{
                    templateUrl: "templates/user_details.html"
                }
            }
        })
        //编辑客户
        .state('menu.list.compile_user', {
            url: "/compile_user/:id",
            views: {
                'dataContent' :{
                    templateUrl: "templates/compile_user.html"
                }
            }
        })
        //客户标签下列表详情
        .state('menu.label.client_list', {
            url: "/client_list/:id",
            views: {
                'labelContent' :{
                    templateUrl: "templates/client_list.html"
                }
            }
        })
         //标签列表
        .state('menu.label.label_details', {
            url: "/label_details/:id/:name",
            views: {
                'labelContent' :{
                    templateUrl: "templates/label_details.html"
                }
            }
        })
       //编辑标签成员
        .state('menu.label.add_label', {
            url: "/add_label/:id",
            views: {
                'labelContent' :{
                    templateUrl: "templates/add_label.html"
                }
            }
        })
        //编辑删除标签
        .state('menu.label.rewrite_label', {
            url: "/rewrite_label/:id/:name",
            views: {
                'labelContent' :{
                    templateUrl: "templates/rewrite_label.html"
                }
            }
        })
        //添加标签
        .state('menu.label.new_label', {
            url: "/new_label",
            views: {
                'labelContent' :{
                    templateUrl: "templates/add_label.html"
                }
            }
        })
        //添加标签成员
        .state('menu.label.add_label_client', {
            url: "/add_label_client/:id",
            views: {
                'labelContent' :{
                    templateUrl: "templates/add_label_client.html"
                }
            }
        })
    }else{
        $stateProvider
        //详情客户
        .state('user_details', {
            url: "/user_details/:id",
            templateUrl: "templates/user_details.html"
        })
        //编辑客户
        .state('compile_user', {
            url: "/compile_user/:id",
            templateUrl: "templates/compile_user.html"
        })
        //标签列表
        .state('label_details', {
            url: "/label_details/:id/:name",
            templateUrl: "templates/label_details.html"
            
        })
        //标签列表
        .state('label_list', {
            url: "/label_list/:id",
            templateUrl: "templates/label_list.html"
            
        })
        //编辑标签成员
        .state('add_label', {
            url: "/add_label/:id",
            templateUrl: "templates/add_label.html"
        })
        //添加标签
        .state('new_label', {
            url: "/new_label",
            templateUrl: "templates/add_label.html"
        })
        //添加标签成员
        .state('add_label_client', {
            url: "/add_label_client/:id",
            templateUrl: "templates/add_label_client.html"
        })
        //添加标签成员
        .state('rewrite_label', {
            url: "/rewrite_label/:id/:name",
            templateUrl: "templates/rewrite_label.html"
        })
    }
    $urlRouterProvider.otherwise('/menu/list');
});

